package com.easylinkin.linkappapi.config.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.config.dao.ConfigMapper;
import com.easylinkin.linkappapi.config.dao.SysDictItemMapper;
import com.easylinkin.linkappapi.config.dao.SysDictMapper;
import com.easylinkin.linkappapi.config.entity.Config;
import com.easylinkin.linkappapi.config.entity.SysDict;
import com.easylinkin.linkappapi.config.entity.SysDictItem;
import com.easylinkin.linkappapi.config.service.SysDictItemService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author chenyin
 * @date 2024/02/18
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysDictItemServiceImpl extends ServiceImpl <SysDictItemMapper, SysDictItem> implements SysDictItemService, ApplicationListener<ApplicationReadyEvent> {

    private final SysDictItemMapper mapper;

    private final SysDictMapper dictMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void syncCache(boolean syncAll) {

    }

    @Override
	public void deleteByDictId(Long dictId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_id_",dictId);
        this.mapper.delete(queryWrapper);
	}

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {

    }

    @Override
    public  SysDictItem selectByDictItem(String dictCode,String itemValue){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_code_", dictCode);
        SysDict sysDict = dictMapper.selectOne(queryWrapper);
        if (Objects.isNull(sysDict)){
            return null;
        }
        QueryWrapper itemQueryWrapper = new QueryWrapper();
        itemQueryWrapper.eq("dict_id_", sysDict.getId());
        itemQueryWrapper.eq("item_value_", itemValue);
        SysDictItem sysDictItem = mapper.selectOne(itemQueryWrapper);
        if (Objects.isNull(sysDictItem)){
            return null;
        }
        return sysDictItem;
    }
    @Override
    public List<SysDictItem> selectByDictItems(String dictCode){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_code_", dictCode);
        SysDict sysDict = dictMapper.selectOne(queryWrapper);
        if (Objects.isNull(sysDict)){
            return null;
        }
        QueryWrapper itemQueryWrapper = new QueryWrapper();
        itemQueryWrapper.eq("dict_id_", sysDict.getId());
        List<SysDictItem> sysDictItems = mapper.selectList(itemQueryWrapper);
        if (Objects.isNull(sysDictItems)){
            return null;
        }
        return sysDictItems;
    }


}